<?php
/*
    BlogEffects 0.1b2
    http://blogeffects.net/
    Copyright (C) 2007 Chad Ohman (chad@chadohman.com) All rights reserved.

    BlogEffects is distributed WITHOUT WARRANTY WHATSOEVER.

    Last developer edit: December 16, 2007 1:18p
*/

function configure() {
	global $mysql;
	mysql_connect($mysql['server'], $mysql['user'], $mysql['pass']) or die("Could not connect: ".mysql_error());
	mysql_select_db($mysql['database']);
}

function fetch_blog_settings() {
	$result = mysql_query("SELECT * FROM options");
	while ($amess = mysql_fetch_array($result)) {
		$options[$amess['option']] = $amess['value'];
		$i++;
	}
	return $options;
}

// Do not edit. This function is critical for BlogEffects to work.
function blog_hash() {
	global $options;
	$chunks = str_split($options['blog_date_activation'], 6);
	$md5['1'] = md5($chunks['2']);
	$md5['2'] = md5($chunks['4']);
	$md5['3'] = md5($chunks['0']);
	$md5['4'] = md5($chunks['3']);
	$md5['5'] = md5($chunks['1']);
	$chunks = null;
	$hashed = md5($md5['3'].$md5['2'].$md5['4'].$md5['1'].$md5['5']);
	$hash = str_split($hashed, 2);
	return $hash['1'];
}

function set_login_cookie() {
	global $_POST;
	$hash = blog_hash();
	setcookie('be_'.$hash, 'logged_in', time()+3600);
	return true;
}

function destroy_login_cookie() {
	$hash = blog_hash();
	$_COOKIE['be_'.$hash] = null;
	setcookie('be_'.$hash, 'logged_out');
	return true;
}

function check_login_cookie() {
	global $config;
	$hash = blog_hash();
	if ($_COOKIE['be_'.$hash] == 'logged_in') {
		return true;
	} else {
		return false;
	}
}

function check_login() {
	$hash = blog_hash();
	if (check_cookie('be_'.$hash)) {
		return true;
	} else {
		return false;
	}
}

function alert($attr) {
	$options = fetch_blog_settings();	
	if ($attr == "comments") {
		return "There are <em>zero</em> comments waiting for your approval.";
	} elseif ($attr == "spam") {
		return "BlogEffects has blocked <em>".numberwords($options['moderation_count_total'])."</em> spam comments from your blog since <em>".date("F j, Y", $options['blog_date_activation'])."</em>.";
	} elseif ($attr == "post_alert") {
		$result = mysql_query("SELECT `timestamp` FROM `posts` ORDER BY `timestamp` DESC LIMIT 1");
		$post = mysql_fetch_array($result);
		$phrase = howlongagowas($post['timestamp'], time());
		return "You have not posted for <em>".$phrase."</em>. Would you like to <a href=\"?post\">post now</a>?";
	} elseif ($attr == "") {
		return "Alerts";
	}
}

function howlongagowas($older_date, $newer_date = false) {
	$chunks = array(
	array(60 * 60 * 24 * 365 , 'year'),
	array(60 * 60 * 24 * 30 , 'month'),
	array(60 * 60 * 24 * 7, 'week'),
	array(60 * 60 * 24 , 'day'),
	array(60 * 60 , 'hour'),
	array(60 , 'minute'),
	);
	
	if ($newer_date == false) {
		$newer_date = time()+(60*60*-7);
	} else {
		$newer_date = $newer_date;
	}
	
	$since = $newer_date - $older_date;
	
	for ($i = 0, $j = count($chunks); $i < $j; $i++) {
		$seconds = $chunks[$i][0];
		$name = $chunks[$i][1];

		if (($count = floor($since / $seconds)) != 0) {
			break;
		}
	}

	$output = ($count == 1) ? '1 '.$name : "$count {$name}s";

	if ($i + 1 < $j) {
		$seconds2 = $chunks[$i + 1][0];
		$name2 = $chunks[$i + 1][1];
		
		if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
			$output .= ($count2 == 1) ? ', 1 '.$name2 : " and, $count2 {$name2}s";
		}
	}
	return $output;
}


function get_posts($numposts) {
	$result = mysql_query("SELECT * FROM `posts` ORDER BY `timestamp` DESC LIMIT ".$numposts);
	if (isset($_GET['edit'])) {
		while ($post = mysql_fetch_array($result)) {
			echo '<tr>';
			echo '<td><a href="?post&edit&id='.$post['id'].'">'.$post['title'].'</a></td>';
			echo '<td>'.date("F j, Y", $post['timestamp']).'</td>';
			echo '</tr>';
		}
	} elseif (isset($_GET['delete'])) {
		while ($post = mysql_fetch_array($result)) {
			echo '<tr>';
			echo '<td><a href="?post&delete&id='.$post['id'].'">'.$post['title'].'</a></td>';
			echo '<td>'.date("F j, Y", $post['timestamp']).'</td>';
			echo '</tr>';
		}
	}
}

function page_attributes($attr) {
	if (isset($_GET['home'])) {
		if ($attr == "text") {
			return "Home";
		} elseif ($attr == "explain") {
			return "Welcome to BlogEffects. This is your personal administration panel where you can post, moderate, and administrate your BlogEffects installation.";
		}
	} elseif (isset($_GET['post'])) {
		if ($attr == "text") {
			return "Post";
		} elseif ($attr == "explain") {
			if (isset($_GET['new'])) {
				if (isset($_POST['submit'])) {
					return 'Your post was successfully entered. Check your <a href="../">blog</a> to see it.';
				} else {
					return 'Enter your post below.';
				}
			} elseif (isset($_GET['edit'])) {
				if (isset($_POST['submit'])) {
					return 'Your post was successfully edited. Check your <a href="../">blog</a> to see it.';
				} else {
					return 'Made a mistake? Edit your post.';
				}
			} elseif (isset($_GET['delete'])) {
				if (isset($_GET['id'])) {
					return 'Your post was successfully deleted. <a href="javascript:history.go(-1);">Go back</a>.';
				} else {
					return 'Be very, very careful. One false move here and your posts go boom.';
				}
			} else {
				return 'You can create, modify, and delete your posts here. Please choose an option from above.';
			}
		} elseif ($attr == "navigation") {
			$link .= '<a href="?post&new">Create a Post</a>'.'&nbsp;|&nbsp;';
			$link .= '<a href="?post&edit">Edit a Post</a>'.'&nbsp;|&nbsp;';
			$link .= '<a href="?post&delete">Delete a Post</a>'.'&nbsp;';
			return $link;
		}
	} elseif (isset($_GET['moderation'])) {
		
	} elseif (isset($_GET['extensions'])) {
		
	} elseif (isset($_GET['api'])) {
		
	} elseif (isset($_GET['administration'])) {
		
	}
}

?>